package com.example.newdemo.view.recyclerView.demo1;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.newdemo.R;
import com.example.newdemo.utils.LogUtils;

import java.util.ArrayList;



/**
 * Demo1：RecyclerView的基本使用。
 * - 为RecyclerView添加OnItemClickListener接口。
 * - ItemDecoration的范例：DividerItemDecoration。
 * - 为RecyclerView实现Headerview和Footerview。
 */
public class RecyclerViewActivity1 extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private ArrayList<ObjectModel> mData;
    private NormalAdapterWrapper mAdapter;
    private NormalAdapter mNoHeaderAdapter;
    private DividerItemDecoration mDecoration;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_recycler_view1);

        mRecyclerView = (RecyclerView) findViewById(R.id.rv);

        mDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
        mLayoutManager = new LinearLayoutManager(this);

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.addItemDecoration(mDecoration);



        //  //      ////////////
        CustomAdapter adapter = new CustomAdapter(initData());

        adapter.setOnClickListener(new CustomAdapter.OnClickListener() {
            @Override
            public void onItemClick(View view, int position) {
//                Toast.makeText(RecyclerViewActivity1.this,"position=" + position, Toast.LENGTH_SHORT).show();
//                Log.d("RecyclerViewActivity",);
                LogUtils.d("  position = " + position);
            }
        });

        //  //      ////////////


        //------------------------------------------------
        //填充数据
        mData=initData();
        mNoHeaderAdapter = new NormalAdapter(mData);

        mAdapter = new NormalAdapterWrapper(mNoHeaderAdapter);

        //添加分割线
//        mDecoration.setDividerDrawable(getResources().getDrawable(R.drawable.divider));

        View headerView = LayoutInflater.from(this).inflate(R.layout.item_header_recycler_view, mRecyclerView, false);
        View footerView = LayoutInflater.from(this).inflate(R.layout.item_footer_recycler_view, mRecyclerView, false);

        mAdapter.addFooterView(footerView);//添加foot
        mAdapter.addHeaderView(headerView);//header

        //------------------------------------------------


        mRecyclerView.setAdapter(adapter);



//        mRecyclerView.set


    }

    public ArrayList<ObjectModel> initData(){
        ArrayList<ObjectModel> models = new ArrayList<>();
        String[] titles = getResources().getStringArray(R.array.title_array);
        for(int i=0;i<titles.length;i++){
            ObjectModel model = new ObjectModel();
            model.number = i + 1;
            model.title = titles[i];
            models.add(model);
        }
        return models;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_1, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){

            case R.id.item_add:
                //添加
                ObjectModel obj = new ObjectModel();
                obj.number = 0;
                obj.title = "Insert";
                mData.add(0,obj);
                mAdapter.notifyItemInserted(1);
                break;

            case R.id.item_delete:
                //删除
                mData.remove(0);
                mAdapter.notifyItemRemoved(1);
                break;

            case R.id.item_change_divider:
                //添加分割线
                mDecoration.setDividerDrawable(getResources().getDrawable(R.drawable.divider));
                mAdapter.notifyDataSetChanged();
                break;
            case R.id.item_hlistview:
                //切换成水平方向滑动
                mRecyclerView.removeItemDecoration(mDecoration);
                mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
                mRecyclerView.setLayoutManager(mLayoutManager);
                mDecoration = new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL_LIST);
                mRecyclerView.addItemDecoration(mDecoration);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
